beverages = [
    ("可乐  ", 3.0),
    ("雪碧  ", 3.0),
    ("矿泉水", 2.0),
    ("橙汁  ", 3.5),
    ("咖啡  ", 4.0)
]

def display_beverages():
    """
    显示饮品信息
    """
    print("欢迎使用无人饮品自动售货机")
    print("饮品列表：")
    for index, (name, price) in enumerate(beverages, start=1):
        print(f"{index}. {name} - 价格：{price}元")

def calculate_total(drinks):
    """
    计算总额
    :param drinks: 一个列表，包含用户选择的饮品索引
    :return: 总金额
    """
    total = sum(beverages[drink - 1][1] for drink in drinks)
    return total

def process_payment(total):
    """
    处理支付
    :param total: 需要支付的总金额
    """
    payment = float(input(f"请支付 {total} 元："))
    if payment >= total:
        change = round(payment - total, 2)
        print(f"支付成功！找零：{change}元")
        return True
    else:
        print("支付金额不足，请重新支付！")
        return False

def main():
    display_beverages()
    selected_drinks = []
    while True:
        try:
            drink_index = int(input("请输入想要购买的饮品编号（输入0完成购买）："))
            if drink_index == 0:
                break
            if 1 <= drink_index <= len(beverages):
                selected_drinks.append(drink_index)
                print(f"已添加{beverages[drink_index - 1][0]}到购物车。")
            else:
                print("饮品编号错误，请重新输入！")
        except ValueError:
            print("请输入有效的编号！")

    total = calculate_total(selected_drinks)
    if process_payment(total):
        print("感谢您的购买！请从出货口取走您的饮品。")
    else:
        print("交易取消。")

if __name__ == "__main__":
    main()






